﻿using MatlabWork.Dal;
using MatlabWork.Entity;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestingPlatForm.MissionContrl.Entity;

namespace TestingPlatForm.MissionContrl.Dal
{
    public class InitialVal
    {
        public static void InitialDic()
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            string logConfigFile = currentDirectory + "\\config\\100km格网信息表（含高度）.xlsx";
            string filename = "";
            if (GlobalEntity.configData.fileName == logConfigFile)
                filename = GlobalEntity.configData.fileName;
            else
                filename = logConfigFile;
            Dictionary<string, Titude> result = new Dictionary<string, Titude>();
            if (GlobalEntity.configData.fileName != null)
            {
                using (ExcelPackage package = new ExcelPackage(new FileInfo(filename)))
                {
                    Calculate calculate = new Calculate();
                    ExcelWorkbook workbook = package.Workbook;
                    if (workbook != null)
                    {
                        ExcelWorksheet worksheet = workbook.Worksheets[0];
                        int rowCount = worksheet.Dimension.Rows;
                        for (int i = 2; i <= rowCount; i++)
                        {
                            Titude titude = new Titude();
                            string Data = worksheet.Cells[i, 2].Value.ToString();
                            string[] strings = Data.Split(",");
                            titude.latitude = double.Parse(strings[0]);
                            titude.longitude = double.Parse(strings[1]);
                            titude.altitude = double.Parse(worksheet.Cells[i, 3].Value.ToString());
                            titude = calculate.ConvertGeodeticToECEF(titude);
                            result[worksheet.Cells[i, 1].Value.ToString()] = titude;
                        }
                    }
                }
            }
            GlobalEntity.TitudeDic = result;
        }

        public static void RefreshValue()
        {
            GlobalEntity.calculateValue.fu = GlobalEntity.configData.fu;
            GlobalEntity.calculateValue.ff = GlobalEntity.configData.ff;
            GlobalEntity.calculateValue.x_st1 = GlobalEntity.configData.x_st1;
            GlobalEntity.calculateValue.y_st1 = GlobalEntity.configData.y_st1;
            GlobalEntity.calculateValue.z_st1 = GlobalEntity.configData.z_st1;
            GlobalEntity.calculateValue.x_st2 = GlobalEntity.configData.x_st2;
            GlobalEntity.calculateValue.y_st2 = GlobalEntity.configData.y_st2;
            GlobalEntity.calculateValue.z_st2 = GlobalEntity.configData.z_st2;
            Titude titude = new Titude();
            GlobalEntity.calculateValue.x_bs = GlobalEntity.configData.x_bs;
            GlobalEntity.calculateValue.y_bs = GlobalEntity.configData.y_bs;
            GlobalEntity.calculateValue.z_bs = GlobalEntity.configData.z_bs;
            titude = XToXVI(int.Parse(GlobalEntity.configData.st));
            GlobalEntity.calculateValue.x_st = titude.X;
            GlobalEntity.calculateValue.y_st = titude.Y;
            GlobalEntity.calculateValue.z_st = titude.Z;
        }

        public static Titude XToXVI(int decimalNumber)
        {
            string hexString = Convert.ToString(decimalNumber, 16);

            Calculate calculate = new Calculate();
            Titude titude = new Titude();
            titude.longitude = GlobalEntity.TitudeDic[hexString].longitude;
            titude.latitude = GlobalEntity.TitudeDic[hexString].latitude;
            titude.altitude = GlobalEntity.TitudeDic[hexString].altitude;
            return calculate.ConvertGeodeticToECEF(titude);
        }

        public static void InitialList()
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            string logConfigFile = currentDirectory + "\\config\\查表库1.xlsx";
            //string filename = "";
            //if (GlobalEntity.configData.fileName == logConfigFile)
            //    filename = GlobalEntity.configData.fileName;
            //else
            //    filename = logConfigFile;
            List<UpData> upDatas = new List<UpData>();
            List<DownData> downDatas = new List<DownData>();
            //if (GlobalEntity.configData.fileName != null)
            //{
            using (ExcelPackage package = new ExcelPackage(new FileInfo(logConfigFile)))
            {
                ExcelWorkbook workbook = package.Workbook;
                if (workbook != null)
                {
                    ExcelWorksheet worksheet = workbook.Worksheets[0];
                    int rowCount = worksheet.Dimension.Rows;
                    for (int i = 2; i <= rowCount; i++)
                    {
                        DownData downData = new DownData();
                        downData.Evaluation = double.Parse(worksheet.Cells[i, 1].Value.ToString());
                        downData.EIRP = double.Parse(worksheet.Cells[i, 2].Value.ToString());
                        downData.GT = double.Parse(worksheet.Cells[i, 3].Value.ToString());
                        downDatas.Add(downData);
                    }

                    worksheet = workbook.Worksheets[1];
                    rowCount = worksheet.Dimension.Rows;
                    for (int i = 2; i <= rowCount; i++)
                    {
                        UpData upData = new UpData();
                        upData.Evaluation = double.Parse(worksheet.Cells[i, 1].Value.ToString());
                        upData.GT = double.Parse(worksheet.Cells[i, 2].Value.ToString());
                        upData.gain = double.Parse(worksheet.Cells[i, 3].Value.ToString());
                        upDatas.Add(upData);
                    }
                }
            }
            //}
            GlobalEntity.upDatas = upDatas;
            GlobalEntity.downDatas = downDatas;
        }
    }
}
